package com.woniu.darryringmall.front.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.darryringmall.admin.Request.GoodsVO;
import com.woniu.darryringmall.front.entity.Goods;
import com.woniu.darryringmall.front.entity.HotGoods;
import com.woniu.darryringmall.front.entity.Images;
import com.woniu.darryringmall.front.entity.ViewGoods;
import com.woniu.darryringmall.front.mapper.GoodsMapper;
import com.woniu.darryringmall.front.mapper.ImagesMapper;
import com.woniu.darryringmall.front.mapper.ViewGoodsMapper;
import com.woniu.darryringmall.front.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ViewGoodsMapper viewGoodsMapper;

    @Autowired
    private ImagesMapper imagesMapper;
    



    @Transactional
    @Override
    public void add(GoodsVO goodsVO) {
        Goods goods = goodsVO.getGoods();
        List<String> urls = goodsVO.getUrlList();
        String no = "DRM" + new Date().getTime();
        goods.setGoodsNo(no);
        goods.setImage(urls.get(0));
        goods.setStatus("y");
        goodsMapper.insert(goods);
        for (String url : urls) {
            Images images = new Images();
            images.setLink(url);
            images.setGoodsId(goods.getId());
            imagesMapper.insert(images);
        }
    }

    @Transactional
    @Override
    public void del(int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("goodsid", id);
        imagesMapper.deleteByMap(map);
        goodsMapper.deleteById(id);
    }

    @Override
    public void conditionUpdate(GoodsVO goodsVO) {
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        Goods goods = goodsVO.getGoods();
        updateWrapper.eq("id", goods.getId());
        updateWrapper.set("name", goods.getName());
        if (goods.getDescription() != null && goods.getDescription().trim().length() != 0) {
            updateWrapper.set("description", goods.getDescription());
        }
        updateWrapper.set("ishot", goods.getIsHot());
        updateWrapper.set("isnew", goods.getIsNew());
        List<String> urls = goodsVO.getUrlList();
        updateWrapper.set("image", urls.get(0));
        //删除原来的图片
        Map<String, Object> map = new HashMap<>();
        map.put("goodsid", goods.getId());
        imagesMapper.deleteByMap(map);
        for (String url : urls) {
            Images images = new Images();
            images.setLink(url);
            images.setGoodsId(goods.getId());
            imagesMapper.insert(images);
        }
        goodsMapper.update(goods, updateWrapper);
    }

    @Override
    public List<HotGoods> hotlist() {
        return goodsMapper.hotlist();
    }

    /**
     * 首页展示最新商品
     * @return
     */
    @Override
    public List<Goods> newGoods() {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isNew", "y");
        queryWrapper.eq("status", "y");
        List<Goods> goods=goodsMapper.selectList(queryWrapper);
        goods.forEach(System.out::println);
        for (Goods g:goods) {
            List<ViewGoods> viewGoods = viewGoodsMapper.getByPrice(g.getId());
            g.setPrice(viewGoods.get(0).getPrice());
        }

        List newGoods = goods.subList(0, 4);
        return newGoods;
    }

    /**
     * 首页展示最热商品
     * @return
     */
    @Override
    public List<Goods> hotGoods() {

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isHot", "y");
        queryWrapper.eq("status", "y");
        List<Goods> goods=goodsMapper.selectList(queryWrapper);
        for (Goods g:goods) {
            List<ViewGoods> viewGoods = viewGoodsMapper.getByPrice(g.getId());
            g.setPrice(viewGoods.get(0).getPrice());
        }
        List hotGoods = goods.subList(0, 4);
        return hotGoods;
    }

    @Override
    public List<Goods> typeGoods(String type) {
        List<Goods> goods=viewGoodsMapper.getByType(type);
        for (Goods g:goods) {
            List<ViewGoods> viewGoods = viewGoodsMapper.getByPrice(g.getId());
            g.setPrice(viewGoods.get(0).getPrice());
        }
        List typeGoods=goods.subList(0,4);
        return typeGoods;
    }


}
